import { parse, print, prettyPrint } from "recast";
let {
  arrayExpression,
  arrowFunctionExpression,
  callExpression,
  stringLiteral,
  identifier,
  objectExpression,
  objectProperty
} = require("recast").types.builders;
let cloneImport = require("recast").types.builders["import"];
let arr = print(
  arrayExpression([
    objectExpression([
      objectProperty(identifier("name"), stringLiteral("7777")),
      objectProperty(identifier("path"), stringLiteral("/views/niupi/niupi")),
      objectProperty(
        identifier("component"),
        arrowFunctionExpression(
          [],
          callExpression(cloneImport(), [stringLiteral("@/views/niupi/niupi")])
        )
      )
    ])
  ])
).code;
export function createObject({ name, path, component, redirect, query }) {
  if (query && Object.keys(query).length > 0) {
    let keys = Object.keys(query);
    path += keys.map((ele) => `/:${ele}`).join("");
  }
  let arr = [
    objectProperty(identifier("name"), stringLiteral(name)),
    objectProperty(identifier("path"), stringLiteral(path)),
    objectProperty(
      identifier("component"),
      arrowFunctionExpression(
        [],
        callExpression(cloneImport(), [stringLiteral(`@${component}`)])
      )
    ),
    objectProperty(identifier("children"), arrayExpression([]))
  ];
  if (redirect) {
    arr.push(objectProperty(identifier("redirect"), stringLiteral(redirect)));
  }
  return objectExpression(arr);
}
// console.log(
//   print(createObject({ name: "123", path: "1231", component: "12312" }))
// );
